package close.mazegame.tile;

import close.mazegame.art.MiscArt;
import close.mazegame.art.tileset.WallTileSet;
import close.mazegame.screens.Screen;
import close.util.BitMask;

import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.World;

public class WallTile extends SolidTile{
	
	private TextureRegion[] textures;
	
	public int mask; //Describes the solidness of the surrounding tiles
	
	private WallTileSet tileSet;
	
	public WallTile(Vector2 pos, WallTileSet tileSet, World world) {
		this(pos, tileSet, world, (byte) 0); //Set mask to zero for now, this will be updated by the level creator
	}
	
	public WallTile(Vector2 pos, WallTileSet tileSet, World world, byte neighbourMask) {
		super(pos, world);
		this.tileSet = tileSet;
		this.textures = new TextureRegion[4];
		setMask(neighbourMask);
	}

	@Override
	public TextureRegion getTexture() {
		return MiscArt.voidTile;
	}
	
	public void render(Screen screen) {	
		for (int i = 0; i < textures.length; i++) {
			float xOffset = (i == 0 || i == 3) ? -1 * Tile.SIZE / 2 : 0;
			float yOffset = (i == 2 || i == 3) ? -1 * Tile.SIZE / 2 : 0;
			screen.draw(textures[i], getPosition().x + xOffset, getPosition().y + yOffset, Tile.SIZE / 2, Tile.SIZE / 2);
		}
		//screen.draw(textures[0], getPosition().x, getPosition().y, Tile.SIZE / 2, Tile.SIZE / 2);
	}
	
	public void setMask(byte mask) {
		this.mask = mask;
		int cornersMasks[] = new int[4];
		cornersMasks[0] = BitMask.getBitStatus(mask, 7)*1 + BitMask.getBitStatus(mask, 0)*2 + BitMask.getBitStatus(mask, 1)*4;
		cornersMasks[1] = BitMask.getBitStatus(mask, 1)*1 + BitMask.getBitStatus(mask, 2)*2 + BitMask.getBitStatus(mask, 3)*4;
		cornersMasks[2] = BitMask.getBitStatus(mask, 3)*1 + BitMask.getBitStatus(mask, 4)*2 + BitMask.getBitStatus(mask, 5)*4;
		cornersMasks[3] = BitMask.getBitStatus(mask, 5)*1 + BitMask.getBitStatus(mask, 6)*2 + BitMask.getBitStatus(mask, 7)*4;
		
		for (int i = 0; i < 4; i++) {
			textures[i] = tileSet.getTexture(cornersMasks[i], i);
		}
	}
}
